<template>
  <div class="way-list">
    <el-card>
      <div class="head-container">
        <!-- 搜索 -->
        <!-- 搜索 -->
        <el-input size="mini"  clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" >搜索</el-button>
        <!-- 新增 -->
        <div style="display: inline-block;margin: 0px 2px;">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="add"
          >新增</el-button>
          <el-button
            type="danger"
            class="filter-item"
            size="mini"
            icon="el-icon-refresh"
          >刷新</el-button>
          <el-button
            type="warning"
            class="filter-item"
            size="mini"
            icon="el-icon-s-operation"
            @click="$refs.initHospital.initVisible=true"
          >初始化医院</el-button>
        </div>
      </div>
      <!-- 初始化医院 -->
      <init-hospital ref="initHospital"></init-hospital>
        <!-- 提交表单 -->
      <eform :isAdd="isAdd" ref="form" @refresh="getList" :locationId="locationId"></eform>
      <!-- 表单数据 -->
      <el-table  border max-height="700"  v-loading="loading" :data="list" size="small" style="width: 100%; margin-top: 20px;">
          <el-table-column prop="locationName" label="医院名称" />
          <el-table-column prop="points" label="两个点的id" />
          
          <el-table-column label="正序导航图名称">
            <template slot-scope="scope">
              <img v-if="scope.row.imageName.length > 0" :src="scope.row.imageName[0][1]" alt="">
            </template>
          </el-table-column>
          <el-table-column label="倒序导航图名" >
            <template slot-scope="scope">
              <img v-if="scope.row.imageNameDesc.length > 0"  :src="scope.row.imageNameDesc[0][1]" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="hint" label="提示" />
          <!-- <el-table-column prop="locationId" label="地点id" /> -->
          <el-table-column prop="dist" label="距离(米)" />
          <el-table-column label="操作" width="205px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
              <span style="display: inline-block;width: 3px;"></span>
              <el-popover
                :ref="scope.row.id"
                placement="top"
                width="180"
              >
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button  :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                </div>
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-popover>
            </template>
          </el-table-column>
      </el-table>
    </el-card>
    <!--分页组件-->
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      background
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import eform from './Form'
import pointsApi from '@/network/map/points.api'
import InitHospital from '../../components/InitHospital.vue'


export default {
  name: 'WayList',
  props: {
    locationId: {
      type: String,
      required: true,
    }
  },
  components: {
    eform,
    InitHospital,
  },
  data() {
    return {
     total: 10,
     page: 1,
     size: 10,
     isAdd: true,
     loading: true,
     delLoading: false,
     imageUrl: '',
     idInput: false,
     list: [
     ],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      let {page, size,locationId} = this
      let res = await pointsApi.getList(page,size,locationId)
      let list = res.pointsList.map(item => {
        if(item.imageName && item.imageName != '') {
          item.imageName = item.imageName.split(';')
          let end = item.imageName.length - 1
          item.imageName[end] == '' && item.imageName.pop()
          item.imageName = item.imageName.map(item => {
            return item.split(':$:')
          })
        } else {
          item.imageName = []
        }
        if(item.imageNameDesc && item.imageNameDesc != '') {
          item.imageNameDesc = item.imageNameDesc.split(';')
          let end = item.imageNameDesc.length - 1
          item.imageNameDesc[end] == '' && item.imageNameDesc.pop()
          item.imageNameDesc = item.imageNameDesc.map(item => {
            return item.split(':$:')
          })
        } else {
          item.imageNameDesc = []
        }
        return item
      })
      this.list = res.pointsList
      this.total = res.Count
       this.loading = false
    },
    sizeChange(size) {
      this.size = size
      this.getList()
    },
    pageChange(page) {
      this.page = page
      this.getList()
    },
    add() {
      this.isAdd = true
      const form = this.$refs.form
      form.isDialog = true
      form.ruleForm.locationId = this.locationId
    },
    edit(data) {
      this.isAdd = false
      let {dist,hint,id,imageName,imageNameDesc,points} = data
      let pstart = points.split(',')[0];
      let pend = points.split(',')[1];
      const form = this.$refs.form
      form.ruleForm.locationId = data.locationId
      form.ruleForm.dist = dist
      form.ruleForm.hint = hint
      form.ruleForm.id = id
      form.ruleForm.images = []
      form.ruleForm.imagesDesc = []
      form.ruleForm.pstart = pstart * 1
      form.ruleForm.pend = pend * 1
      form.imgList = imageName.map(item => {
        return {name: item[0], url: item[1]}
      })
      form.imgDescList = imageNameDesc.map(item => {
        return {name: item[0], url: item[1]}
      })
      form.isDialog = true
    },
    subDelete(id) {
      this.delLoading = true
      pointsApi.delPoints(id).then(() => {
      
        this.delLoading = false
        this.$refs[id].doClose()
        this.getList();
        this.$message({message: '删除成功', type: 'success'})
        
      }).catch(() => {
        this.delLoading = false
        this.$refs[id].doClose()
      })
    }
  },
  watch: {
    locationId(val) {
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.way-list {
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: space-between;
}
.el-card {
  flex: 1;
}
img {
  width: 50px;
  height: auto;
}
.head-container .filter-item {
      display: inline-block;
      vertical-align: middle;
    margin: 0 2px 10px 0;
}
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
    border: 0.5px dashed #8c939d;
    border-radius: 5px;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>